using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace C4F.DevKit.WebServices
{
    /// <summary>
    /// Provides useful methods like conversion methods.
    /// </summary>
    public static class Utility
    {
        /// <summary>
        /// The Method converts the temperature in Fahrenheit to Celcius
        /// </summary>
        /// <param name="temperature">Temperature in Fahrenheit</param>
        /// <returns>Temperature in Celcius</returns>
        public static double FahrenheitToCelcius(double temperature)
        {
            return (5.0 / 9.0) * (temperature - 32);
        }

        /// <summary>
        /// The Method converts the temperature in Celcius to Fahrenheit
        /// </summary>
        /// <param name="temperature">Temperature in Celcius</param>
        /// <returns>Temperature in Fahrenheit</returns>
        public static double CelciusToFahrenheit(double temperature)
        {
            return (9.0 / 5.0) * temperature + 32;
        }

        /// <summary>
        /// Validates the zip code
        /// </summary>
        /// <param name="zipCode">Zip code</param>
        /// <returns>True if zip code is valid</returns>
        public static bool ValidateZipCode(string zipCode)
        {
            if (String.IsNullOrEmpty(zipCode))
                return false;

            return Regex.IsMatch(zipCode, "(^[0-9]{5}$)|(^[0-9]{5}-[0-9]{4}$)");
        }

        /// <summary>
        /// Validates the phone number 
        /// [Example formats: '212-123-4567', '(212)123-4567', '2121234567', '(212) 123-4567', '(212)-123-4567']
        /// </summary>
        /// <param name="phoneNumber">Phone number</param>
        /// <returns>True if phone number is valid</returns>
        public static bool ValidatePhoneNumber(string phoneNumber)
        {
            if (String.IsNullOrEmpty(phoneNumber))
                return false;

            return Regex.IsMatch(phoneNumber, "^[(]?[2-9][0-9]{2}[)]?[ -]?[0-9]{3}[ -]?[0-9]{4}$");
        }

        /// <summary>
        /// Validates the area code
        /// </summary>
        /// <param name="areaCode">Area code to be validated. Area code should not start with a 0 or a 1.</param>
        /// <returns>True, if area code is valid</returns>
        public static bool ValidateAreaCode(string areaCode)
        {
            if (String.IsNullOrEmpty(areaCode))
                return false;

            return Regex.IsMatch(areaCode, "^[^01][0-9]{2}$");
        }

        /// <summary>
        /// Validates the exchange code
        /// </summary>
        /// <param name="exchangeCode">Exchange code to be validated</param>
        /// <returns>True, if exchange code is valid</returns>
        public static bool ValidateExchangeCode(string exchangeCode)
        {
            if (String.IsNullOrEmpty(exchangeCode))
                return false;

            return Regex.IsMatch(exchangeCode, "^[0-9]{3}$");
        }

        /// <summary>
        /// Validates the local 4 digit phone number
        /// </summary>
        /// <param name="phone">Phone number to be validated</param>
        /// <returns>True, if phone number is valid</returns>
        public static bool ValidateLocalPhoneNumber(string phone)
        {
            if (String.IsNullOrEmpty(phone))
                return false;

            return Regex.IsMatch(phone, "^[0-9]{4}$");
        }
    }
}
